﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient; 
using System.Configuration;
using System.IO;                                                                                            
using System.Drawing.Imaging;                                                                               



namespace 门诊管理系统
{
    public partial class casehistory : Form
    {
        private string PhotoFileName;

        public casehistory()
        {
            InitializeComponent();
        }

        private void btn_load_Click(object sender, EventArgs e)
        {
            txb.xm = txb_xm.Text.Trim();
            txb.id = txb_id.Text.Trim();
            txb.idcard = txb_jzid.Text.Trim();
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString =
                 ConfigurationManager.ConnectionStrings["connect"].ConnectionString;
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandText = "SELECT * FROM tb_patient  WHERE (IDcard='" +this.txb_jzid.Text.Trim()+  "')OR (ID='" +this.txb_id.Text.Trim()+ "')";
            sqlConnection.Open();
            SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
            byte[] photoBytes = null;

            int
                IDcardOrdinal = sqlDataReader.GetOrdinal("IDcard")
                , nameOrdinal = sqlDataReader.GetOrdinal("name")
                , sexOrdinal = sqlDataReader.GetOrdinal("sex")
                , birthDateOrdinal = sqlDataReader.GetOrdinal("birthdate")
                , IDOrdinal = sqlDataReader.GetOrdinal("ID")
                , phoneOrdinal = sqlDataReader.GetOrdinal("phone");
      
            if (sqlDataReader.Read())
            {
                this.txb_jzid.Text = sqlDataReader.GetString(IDcardOrdinal);
                this.txb_xm.Text = sqlDataReader.GetString(nameOrdinal);
                this.txb_brithday.Text = sqlDataReader.GetDateTime(birthDateOrdinal).ToShortDateString();
                this.txb_id.Text = sqlDataReader.GetString(IDOrdinal);
                this.txb_phone.Text = sqlDataReader.GetString(phoneOrdinal);
                photoBytes =
                    (sqlDataReader["photo"] == DBNull.Value ? null : (byte[])sqlDataReader["photo"]); 
                if (sexOrdinal == 1)
                {
                    this.nan.Checked =true ;
                   
                }
                else
                {

                    this.nv.Checked = true;
                  
                }
            }
            else {
                MessageBox.Show("查无此人");
            };


            sqlDataReader.Close();
        
        if (photoBytes!= null)                                                                         
            {
                MemoryStream memoryStream = new MemoryStream(photoBytes);                                 
                this.zhaopian.Image = Image.FromStream(memoryStream);                                      
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openPhotoDialog = new OpenFileDialog()                                           
            {                                                                                           
                Title = "打开照片文件（位图格式）"                                                      
                ,
                Filter = "BMP Files (*.jpg)|*.jpg"                                                   
                ,
                InitialDirectory = @"C:\"                                                            
            };
            if (openPhotoDialog.ShowDialog() == DialogResult.OK)                                            
            {
                this.PhotoFileName = openPhotoDialog.FileName;                                              
                this.zhaopian.Image = Image.FromFile(this.PhotoFileName);                               
            }
        }

        private void save_Click(object sender, EventArgs e)
        {
            MemoryStream memoryStream = new MemoryStream();                                                 
            this.zhaopian.Image.Save(memoryStream, ImageFormat.Bmp);                                       
            byte[] photoBytes = new byte[memoryStream.Length];                                             
            memoryStream.Seek(0, SeekOrigin.Begin);                                                        
            memoryStream.Read(photoBytes, 0, photoBytes.Length);
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString =
                 ConfigurationManager.ConnectionStrings["connect"].ConnectionString;
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandText = "UPDATE tb_patient"
                + " SET ,IDcard=@IDcard,photo=@photo"
                + " WHERE IDcard=@IDcard;";
            sqlCommand.Parameters.AddWithValue("@IDcard", this.txb_jzid.Text.Trim());
            sqlCommand.Parameters.AddWithValue("@photo", this .zhaopian .Image  );
            sqlConnection.Open();                                                                           
            int rowAffected = sqlCommand.ExecuteNonQuery();                                                
            sqlConnection.Close();
            if (rowAffected == 1)
            {
                MessageBox.Show("上传照片成功");
            }                                     
        }

        private void 过敏史ToolStripMenuItem_Click(object sender, EventArgs e)
        {
           gm g = new gm ();
           patient p = new patient();
           p.Hide();
           g.Show();
        }

        private void 住院记录ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            zyjl z = new zyjl();
            patient p = new patient();
            z.Show();
            p.Hide();
        }

        private void 就诊记录ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            jiuyijilu j = new jiuyijilu ();
            patient p = new patient();
            j.Show();
            p.Hide();
        }
    }
}




